<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            background: #f5f5f5;
            margin: 0;
            padding: 20px;
        }
        
        .container {
            background: white;
            padding: 30px;
            border: 1px solid #ddd;
            max-width: 500px;
            margin: 50px auto;
        }
        
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 1.5em;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: bold;
        }
        
        input[type="text"], input[type="password"], input[type="email"], input[type="tel"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            font-size: 14px;
        }
        
        input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focus, input[type="tel"]:focus {
            outline: none;
            border-color: #007bff;
        }
        
        .sms-group {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        
        .sms-group input {
            flex: 1;
        }
        
        .btn {
            width: 100%;
            padding: 10px;
            background: #007bff;
            color: white;
            border: none;
            font-size: 14px;
            cursor: pointer;
            margin-bottom: 10px;
        }
        
        .btn:hover {
            background: #0056b3;
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .btn-secondary:hover {
            background: #545b62;
        }
        
        .btn-sms {
            background: #28a745;
            white-space: nowrap;
            min-width: 120px;
        }
        
        .btn-sms:hover {
            background: #1e7e34;
        }
        
        .message {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .back-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .back-link a {
            color: #667eea;
            text-decoration: none;
        }
        
        .back-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>用户注册</h1>
        
        <div id="message"></div>
        
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <div class="form-group">
                <label for="email">邮箱</label>
                <input type="email" id="email" name="email" required>
            </div>
            
            <div class="form-group">
                <label for="phone">手机号</label>
                <div class="sms-group">
                    <input type="tel" id="phone" name="phone" placeholder="请输入手机号" required maxlength="11" pattern="^1[3-9]\d{9}$" style="width: calc(100% - 120px);">
                    <button type="button" class="btn btn-sms" onclick="sendSmsCode()" style="width: 110px;">发送验证码</button>
                </div>
            </div>
            
            <div class="form-group">
                <label for="smsCode">短信验证码</label>
                <input type="text" id="smsCode" name="smsCode" placeholder="请输入验证码" required>
            </div>
            
            <button type="submit" class="btn">注册</button>
        </form>
        
        <div class="back-link">
            <a href="/login">已有账号？去登录</a>
        </div>
    </div>

    <script>
        
        function showMessage(message, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = message;
            messageDiv.className = 'message ' + type;
        }
        
        function sendSmsCode() {
            const phone = document.getElementById('phone').value;
            
            if (!phone) {
                showMessage('请先输入手机号', 'error');
                return;
            }
            
            if (!/^1[3-9]\d{9}$/.test(phone)) {
                showMessage('手机号格式不正确', 'error');
                return;
            }
            
            fetch('/sendSmsCode', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'phone=' + encodeURIComponent(phone)
            })
            .then(response => response.text())
            .then(data => {
                showMessage(data, 'success');
            })
            .catch(error => {
                showMessage('发送失败，请重试', 'error');
            });
        }
        
        document.getElementById('registerForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            
            fetch('/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams(formData)
            })
            .then(response => response.text())
            .then(data => {
                if (data === '注册成功') {
                    showMessage('注册成功！请登录', 'success');
                    setTimeout(() => {
                        window.location.href = '/login';
                    }, 2000);
                } else {
                    showMessage(data, 'error');
                }
            })
            .catch(error => {
                showMessage('注册失败，请重试', 'error');
            });
        });
    </script>
</body>
</html>
